# encoding:utf-8
# !/usr/bin/python3
# @AUTHOR : XcNgg
# @IDE:PyCharm 2022.1
# @PROJECT:Net Crawler
# @FILE:题库查询.py
# @EMAIL:XiaoCaoNgg@gamil.com

import time
import loguru
import random
import pymysql
def mysql_connect(password):
    try:
        print("正在连接数据库...")
        config = {
            "user": "answer",
            "password": password,
            "host": "203.25.208.205",
            "db": "answer",
            "charset": "utf8mb4"
        }
        mysql = pymysql.Connection(**config,autocommit=True)
        cursor =mysql.cursor()
        print("连接成功！")
        return cursor
    except:
        print("密码错误！请检查密码是否更新！")
        print("【更新地址】https://gitee.com/grass-likes-to-eat-nangua/wlcy/blob/master/README.md")
        return 1

def select_sql(cursor,title):
    # 题目查询
    selecttt = f"select title from wlcy where title like '%{title}%'; "
    cursor.execute(selecttt)
    print(f"【1】【题目列表】{cursor.fetchall()}")
    selectstr = f"select true_answer from wlcy where title like '%{title}%'; "
    cursor.execute(selectstr)
    result = cursor.fetchall()
    if len(result) == 0:
        try:
            start = random.randint(0, len(title))
            end = len(title) - start
            selecttt = f"select title from wlcy where title like '%{title[start:end]}%'; "
            cursor.execute(selecttt)
            print(selecttt)
            print(f"【2】【题目列表】{cursor.fetchall()}")
            tryagain = f"select true_answer from wlcy where title like '%{title[start:end]}%'; "
            cursor.execute(tryagain)
            result = cursor.fetchall()
            if len(result) != 0:
                return result
        except:
            start = random.randint(0, len(title))
            selecttt = f"select title from wlcy where title like '%{title[start:]}%'; "
            cursor.execute(selecttt)
            print(f"【3】【题目列表】{cursor.fetchall()}")
            tryagain = f"select true_answer from wlcy where title like '%{title[start:-1]}%'; "
            cursor.execute(tryagain)
            result = cursor.fetchall()
            if len(result) != 0:
                return result
            else:
                # 没有查询结果
                print("未找到题库答案!")
                return 1
    else:
        return result



if __name__ == '__main__':
    print("请确保本地库已经成功安装【(pip install loguru pymysql)】/【(pip3 install loguru pymysql)】")
    try:
        loguru.logger.info("手敲题库(目前已收录300+ 2022.5.21) 题库更新则密码更新 欢迎补充 --> XiaoCaoNgg@gamil.com")
        loguru.logger.info("如需自动答题脚本,请联系邮箱")
    except:
        print("请确保本地库已经成功安装【(pip install loguru pymysql)】/【(pip3 install loguru pymysql)】")
    time.sleep(0.3)
    password = input("【请输入登录密码】").replace("\n","")
    cursor = mysql_connect(password)
    while cursor ==1:
        password = input("【请输入登录密码】").replace("\n", "")
        cursor = mysql_connect(password)
    while True:
        title = input("题目:")
        print(f"【题库答案】{select_sql(cursor,title)}")

